﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;
namespace Zor_Bll
{
    /// <summary>
    /// 租赁管理
    /// </summary>
    /// create by liuyunfei at 2017/03/24
    public class Zor_Lease_Bll<T> where T : class
    {
        /// <summary>
        /// 获取租赁列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model, int index, out int TotalCount,string where)
        {
            var uid = RequestSession.GetSessionUser().User_Id;
            var sqlSurch = string.Empty;
            if (Model.Hs_Number != null)
            {
                sqlSurch += " Hs_Number like '%" + Model.Hs_Number + "%' and ";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += "NatPer_Name like '%" + Model.NatPer_Name + "%' and ";
            }
            if (Model.Nature != null)
            {
                sqlSurch += "Nature = '" + Model.Nature + "' and ";
            }
            if (Model.Tenant_Name != null)
            {
                sqlSurch += "Tenant_Name like '%" + Model.Tenant_Name + "%' and ";
            }
            if (Model.Lease_StartTime != null)
            {
                int st = TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_StartTime.ToString()));
                sqlSurch += "Lease_StartTime = " + st + " and ";
            }
            if (Model.Lease_EndTime != null)
            {
                int et = TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_EndTime.ToString()));
                sqlSurch += "Lease_EndTime = " + et + " and ";
            }
            if (Model.statue != null && Model.statue != "全部")
            {
                sqlSurch += "statue = '" + Model.statue + "' and ";
            }
            sqlSurch += " 1=1 and (User_Id = " + uid + "  or  Dt_Id = " + RequestSession.GetSessionUser().DT_Id + ")";
            // sqlSurch += " 1=1 and Dt_Id = " + RequestSession.GetSessionUser().DT_Id + "";
            //return PagerHelp<T>.GetPagerList(index, "dbo.V_Lease", " Bd_Name collate Chinese_PRC_CS_AS_KS_WS", out TotalCount, sqlSurch);
            return PagerHelp<T>.GetPagerList(index, "dbo.V_Lease", "Lease_CreateTime desc ", out TotalCount, sqlSurch);
        }

        /// <summary>
        /// 租赁管理-修改
        /// </summary>
        /// <param name="lease">租赁对象</param>
        /// <returns>应答</returns>
        public static BaseAnswer UpdateLease(Zor_Lease lease, DateTime startTime, DateTime endTime)
        {            
            BaseAnswer bar = Zor_Lease_DAL<T>.UpdateLease(lease, startTime, endTime);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "修改成功！" : "修改失败,此租赁信息已产生受理单！";
            return bar;
        }

        /// <summary>
        /// 租赁管理-新增
        /// </summary>
        /// <param name="lease">租赁对象</param>
        /// <returns>执行结果</returns>
        public static BaseAnswer InsertLease(Zor_Lease lease, DateTime startTime, DateTime endTime)
        {
            BaseAnswer bar = Zor_Lease_DAL<T>.InsertLease(lease, startTime, endTime);            
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "增加成功！" : "增加失败，请检查是否已存在正常租赁关系！";
            return bar;
        }

        /// <summary>
        /// 租赁信息删除
        /// </summary>
        /// <param name="id">租赁信息ID集合</param>
        /// <returns></returns>
        public static BaseAnswer DeleteLease(int[] id)
        {
            BaseAnswer bar = Zor_Lease_DAL<T>.DeleteLease(id);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "删除成功！" : "删除失败，此租赁信息已产生受理单！";
            return bar;
        }

        /// <summary>
        /// 查询所有房号
        /// </summary>
        /// <returns></returns>
        public static List<T> GetHsNumber()
        {
            return Zor_Lease_DAL<T>.GetHsNumber();
        }

        /// <summary>
        /// 查询所有租户
        /// </summary>
        /// <returns></returns>
        public static List<T> GetTenant()
        {
            return Zor_Lease_DAL<T>.GetTenant();
        }

        /// <summary>
        /// 获取租赁管理列表（不分页）
        /// create 刘云飞 by 2017/04/06       
        /// </summary>
        /// <returns></returns>
        public static List<T> GetList(dynamic Model, string where)
        {
            var sqlSurch = string.Empty;
            if (Model.Hs_Number != null)
            {
                sqlSurch += " Hs_Number like '%" + Model.Hs_Number + "%' and ";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += "NatPer_Name like '%" + Model.NatPer_Name + "%' and ";
            }
            if (Model.Nature != null)
            {
                sqlSurch += "Nature = '" + Model.Nature + "' and ";
            }
            if (Model.Tenant_Name != null)
            {
                sqlSurch += "Tenant_Name like '%" + Model.Tenant_Name + "%' and ";
            }
            if (Model.Lease_StartTime != null)
            {
                int st = TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_StartTime.ToString()));
                sqlSurch += "Lease_StartTime = " + st + " and ";
            }
            if (Model.Lease_EndTime != null)
            {
                int et = TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_EndTime.ToString()));
                sqlSurch += "Lease_EndTime = " + et + " and ";
            }
            if (Model.statue != null && Model.statue != "全部")
            {
                sqlSurch += "statue = '" + Model.statue + "' and ";
            }
            sqlSurch += " 1=1 ";
            return Zor_Lease_DAL<T>.SelectViewLease(sqlSurch);
        }

      
        /// <summary>
        /// 获取树结构楼宇信息
        /// </summary>
        /// <returns></returns>
        public static List<T> GetBuildname(int Hs_Falg)
        {
            return Zor_Lease_DAL<T>.GetBuildname(Hs_Falg);
        }

        /// <summary>
        /// 获取树结构单元信息
        /// </summary>
        /// <returns></returns>
        public static List<T> GetUimName(int Bd_Id)
        {
            return Zor_Lease_DAL<T>.GetUimName(Bd_Id);
        }
        
        /// <summary>
        /// 获取树结构的房屋信息GetHousUse
        /// </summary>
        /// <returns></returns>
        public static List<T> GetHousName(int Uim_Id)
        {
            return Zor_Lease_DAL<T>.GetHousName(Uim_Id);
        }

        /// <summary>
        /// 获取房屋用途
        /// </summary>
        /// <returns></returns>
        public static List<T> GetHousUse(int Hs_Id)
        {
            return Zor_Lease_DAL<T>.GetHousUse(Hs_Id);
        }
      
      
    }
}
